package com.mengxiangnongfu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mengxiangnongfu.constants.Constants;
import com.mengxiangnongfu.domain.DictType;
import com.mengxiangnongfu.dto.DeptDto;
import com.mengxiangnongfu.vo.DataGridView;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mengxiangnongfu.domain.Dept;
import com.mengxiangnongfu.mapper.DeptMapper;
import com.mengxiangnongfu.service.DeptService;

/**
 * @author Yang
 * @version 1.0
 * @date 2020/10/25 15:48
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Resource
    private DeptMapper deptMapper;

    @Override
    public DataGridView loadAllDeptByPage(DeptDto deptDto) {
        Page<Dept> page = new Page<>(deptDto.getPageNum(), deptDto.getPageSize());
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(deptDto.getDeptName()), Dept.COL_DEPT_NAME, deptDto.getDeptName());
        queryWrapper.eq(StringUtils.isNotBlank(deptDto.getStatus()), Dept.COL_STATUS, deptDto.getStatus());
        queryWrapper.ge(deptDto.getBeginTime() != null, DictType.COL_CREATE_TIME, deptDto.getBeginTime());
        queryWrapper.le(deptDto.getEndTime() != null, DictType.COL_CREATE_TIME, deptDto.getEndTime());
        queryWrapper.orderByDesc(Dept.COL_CREATE_TIME);
        this.deptMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public List<Dept> loadAllDept() {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper();
        //查询全部有效部门
        queryWrapper.eq(Dept.COL_STATUS, Constants.DEPT_STATUS_ENABLE);
        queryWrapper.orderByDesc(Dept.COL_CREATE_TIME);
        return this.deptMapper.selectList(queryWrapper);
    }

    @Override
    public Dept loadDeptById(Long deptId) {
        return this.deptMapper.selectById(deptId);
    }

    @Override
    public Integer insertDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptDto, dept);
        //设置创建人创建时间
        dept.setCreateBy(deptDto.getSimpleUser().getUserName());
        dept.setCreateTime(DateUtil.date());
        return this.deptMapper.insert(dept);
    }

    @Override
    public Integer modifyDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptDto, dept);
        dept.setUpdateBy(deptDto.getSimpleUser().getUserName());
        dept.setUpdateTime(DateUtil.date());
        return this.deptMapper.updateById(dept);
    }

    @Override
    public Integer removeDeptByIds(Long[] ids) {
        return (null != ids && ids.length > 0) ? this.deptMapper.deleteBatchIds(Arrays.asList(ids)) : 0;
    }

}


